//
//  ZFileHandle.m
//  Test
//
//  Created by 徐益智 on 16/4/10.
//  Copyright © 2016年 Vdianjing. All rights reserved.
//

#import "ZFileHandle.h"
#import "ZConstant.h"

@implementation ZFileHandle

+ (NSString*)generateUuidString
{
    CFUUIDRef uuid_ref = CFUUIDCreate(NULL);
    CFStringRef uuid_string_ref= CFUUIDCreateString(NULL, uuid_ref);
    
    CFRelease(uuid_ref);
    NSString *uuid = [NSString stringWithString:(__bridge NSString*)uuid_string_ref];
    
    CFRelease(uuid_string_ref);
    return uuid;
}

+ (NSString *)gotFilePath
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return paths[0];
}

//保存图片到本地 Document，成功后返回本地文件名
+ (NSString *)saveImageToLocalDocument:(NSData *)imageData
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *fileName = [[ZFileHandle generateUuidString] stringByAppendingString:@".jpg"];
    NSString *documentDirectory = [ZFileHandle gotFilePath];
    NSString *imagePath = [NSString stringWithFormat:@"%@/image", documentDirectory];
    NSString *filePath = [imagePath stringByAppendingPathComponent:fileName];
    
    // 判断文件夹是否存在，如果不存在，则创建
    if (![[NSFileManager defaultManager] fileExistsAtPath:imagePath]) {
        [fileManager createDirectoryAtPath:imagePath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    
    BOOL suc = [fileManager createFileAtPath:filePath contents:imageData attributes:nil];
    if (suc) {
        return fileName;
    }else{
        return nil;
    }
}

//通过文件名获取图片,文件不存在，返回nil
+ (NSString *)fetchLocalDocumentImage:(NSString *)imageName
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *filePath = [ZFileHandle gotFilePath];
    NSString *imagePath = [NSString stringWithFormat:@"%@/image", filePath];
    NSString *imageFilePath = [imagePath stringByAppendingPathComponent:imageName];
    MyLog(@"imagePath = %@", imageFilePath);
    if ([fileManager fileExistsAtPath:imageFilePath]) {
        return imageFilePath;
    }else{
        return nil;
    }
}

+ (void)deleteFileByFileName:(NSString *)name
{
    if ([name isEqualToString:@""])
    {
        return;
    }
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *documentsDirectory = [ZFileHandle gotFilePath];
    NSString *delelteFilePath = [documentsDirectory stringByAppendingPathComponent:name];
    
    NSError *error;
    if ([fileManager removeItemAtPath:delelteFilePath error:&error] != YES)
        MyLog(@"Unable to delete file: %@", [error localizedDescription]);
}

+ (void)deleteImage
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *filePath = [ZFileHandle gotFilePath];
    NSString *imagePath = [NSString stringWithFormat:@"%@/image", filePath];
    [fileManager removeItemAtPath:imagePath error:nil];
}

+ (NSString *)getFileName
{
    NSString * fileName;
    NSDate *datenow = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:datenow];
    NSDate *localeDate = [datenow  dateByAddingTimeInterval: interval];
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[localeDate timeIntervalSince1970]];
    NSLog(@"timeSp:%@",timeSp); //时间戳的值
    NSInteger randomNum = arc4random()%899+100;//100-998
    fileName = [NSString stringWithFormat:@"%@_%ld",timeSp,(long)randomNum];
    return fileName;
}
@end
